package com.iexbuy.ihg.ui.fragment;

import android.os.Bundle;
import android.text.Editable;
import android.text.Selection;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.baidu.navisdk.util.common.StringUtils;
import com.iexbuy.ihg.App;
import com.iexbuy.ihg.R;
import com.iexbuy.ihg.common.CommUtil;
import com.iexbuy.ihg.entity.YunDealObject;
import com.iexbuy.ihg.entity.YunItemListObject;
import com.iexbuy.ihg.net.ServiceHelper;
import com.iexbuy.ihg.utils.JsonUtils;
import com.iexbuy.ihg.utils.StringUtil;
import com.iexbuy.ihg.utils.ToastUtils;
import com.jingchen.pulltorefresh.PullToRefreshLayout;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

import java.util.HashMap;

public class YunGuoOrderFragment extends BaseFragment {
	
	private static YunGuoOrderFragment homeFragmentInstent;

	@ViewInject(R.id.msgListLayout)
	private PullToRefreshLayout msgListRefreshLayout;
	@ViewInject(R.id.listView)
	private ListView listView;

	@ViewInject(R.id.productIcon)
	private ImageView productIcon;
	@ViewInject(R.id.productName)
	private TextView productName;
	@ViewInject(R.id.orderDetail)
	private TextView orderDetail;
	@ViewInject(R.id.buyNumber)
	private EditText buyNumber;

	private static YunItemListObject yunItemListObject;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		homeFragmentInstent = this;
	}

	@Override
	protected View _onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_yunguo_order, container, false);
		ViewUtils.inject(this, rootView);
		initView(rootView);
		return rootView;
	}

	private void initView(View rootView) {
		mBaseActivity.setTitle(getResources().getString(R.string.yun_guo_order_confirm));
		mBaseActivity.setTitleLayoutVisibility(true);
		mBaseActivity.setHomeActivity(false);

		Editable etext = buyNumber.getText();
		Selection.setSelection(etext, etext.length());

		buyNumber.setOnFocusChangeListener(new View.OnFocusChangeListener() {
			@Override
			public void onFocusChange(View view, boolean hasFocus) {
				if (hasFocus) {
					String str =buyNumber.getText().toString();
					Selection.setSelection(buyNumber.getText(), buyNumber.getText().length());
					buyNumber.setSelectAllOnFocus(true);
				} else if (StringUtil.isBlank(buyNumber.getText().toString())) {
					buyNumber.setText("1");
				}
			}
		});
		buyNumber.clearFocus();

		initOrderInfo();
	}

	private void initOrderInfo() {
		if (yunItemListObject == null) return;

		App.bitmapUtils.display(productIcon, yunItemListObject.getThumbnailUrl());
		productName.setText("["+yunItemListObject.getPeriod()+"] "+yunItemListObject.getItemName());
		orderDetail.setText("总共" + yunItemListObject.getRequiredTotalNumber() + "人次 还剩余：" + (Integer.valueOf(yunItemListObject.getRequiredTotalNumber()) - Integer.valueOf(yunItemListObject.getAttandanceNumber())));
	}


	//点击事件响应
	@OnClick(R.id.submitYunguoBtn)
	public void onSubmitYunguoBtnClick(View view) {
		if (StringUtils.isEmpty(buyNumber.getText().toString())) {
			ToastUtils.toast(mBaseActivity, "请输入参与数量");
			return;
		} else if (!StringUtil.isDigit(buyNumber.getText().toString())) {
			ToastUtils.toast(mBaseActivity, "请输入数字类型的参与数量");
			buyNumber.getText().clear();
			return;
		}

		YunGuoOrderConfirmFragment.setYunItemListObject(yunItemListObject);
//		YunGuoOrderConfirmFragment.setBuyNo(Integer.valueOf(buyNumber.getText().toString()));
		CommUtil.openSingleFragmentActivity(mBaseActivity, YunGuoOrderConfirmFragment.class);

	}


	public static void setYunItemListObject(YunItemListObject yunItemListObject) {
		YunGuoOrderFragment.yunItemListObject = yunItemListObject;
	}

	/**
	 * @return the homeFragmentInstent
	 */
	public static YunGuoOrderFragment getHomeFragmentInstent() {
		return homeFragmentInstent;
	}

}
